<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ECharts Pie with Leaflet</title>

    <link rel="stylesheet" href="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.css">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>

<script type="text/javascript" src="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.js"></script>
<script type="text/javascript" src="http://cdn.bootcss.com/echarts/3.6.2/echarts.min.js"></script>
<script type="text/javascript" src="../../dist/iclient9-leaflet.min.js"></script>
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script>


    var map, resultLayer, url = "http://support.supermap.com.cn:8090/iserver/services/map-world/rest/maps/%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE_Gray";
    var url2 = "http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China";
    map = L.map('map', {
        crs: L.CRS.EPSG4326,
        center: [39.905, 116.402],
        maxZoom: 18,
        zoom: 5
    });
    var categoryDataMap = {
        "北京市": [
            {value: 335, name: '类目A'},
            {value: 679, name: '类目B'},
            {value: 1299, name: '类目C'}
        ],
        "广州市": [
            {value: 117, name: '类目A'},
            {value: 1267, name: '类目B'},
            {value: 1048, name: '类目C'}
        ], "上海市": [
            {value: 475, name: '类目A'},
            {value: 1450, name: '类目B'},
            {value: 875, name: '类目C'}
        ]
    };
    var dataMap = {
        "北京市": [
            {value: 335, name: '商品A'},
            {value: 310, name: '商品B'},
            {value: 234, name: '商品C'},
            {value: 135, name: '商品D'},
            {value: 1048, name: '商品E'},
            {value: 251, name: '商品F'}
        ],
        "广州市": [
            {value: 117, name: '商品A'},
            {value: 284, name: '商品B'},
            {value: 768, name: '商品C'},
            {value: 215, name: '商品D'},
            {value: 901, name: '商品E'},
            {value: 148, name: '商品F'}
        ], "上海市": [
            {value: 475, name: '商品A'},
            {value: 29, name: '商品B'},
            {value: 430, name: '商品C'},
            {value: 981, name: '商品D'},
            {value: 732, name: '商品E'},
            {value: 143, name: '商品F'}
        ]
    };
    L.supermap.tiledMapLayer(url).addTo(map);
    var option = {
        title: {
            text: '北京市商品销量',
            subtext: '纯属虚构',
            textStyle: {
                color: '#fff',
                fontSize: 16
            }
        },
        backgroundColor: '#404a59',
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'right',
            y: 'bottom',
            textStyle: {
                color: '#fff',
                fontSize: 12
            },
            data: ['商品A', '商品B', '商品C', '商品D', '商品E', '商品F']
        },
        series: [
            {
                name: '商品类目',
                type: 'pie',
                selectedMode: 'single',
                radius: [0, '30%'],

                label: {
                    normal: {
                        position: 'inner'
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data: categoryDataMap['北京市']
            },
            {
                name: '商品',
                type: 'pie',
                radius: ['40%', '55%'],
                data: dataMap['北京市']
            }
        ]
    };
    var div = L.DomUtil.create('div');
    var chart = echarts.init(div, '', {
        width: 500,
        height: 300
    });
    chart.setOption(option);
    var control = L.control({position: 'topright'});
    control.onAdd = function (map) {
        return chart.getDom();
    };
    control.addTo(map);
    query();
    function query() {
        clearLayer();
        var param = new SuperMap.QueryBySQLParameters({
            queryParams: [{
                name: "China_ProCenCity_pt@China",
                attributeFilter: "NAME = '广州市' or NAME = '上海市'"
            }, {
                name: "China_Capital_pt@China",
                attributeFilter: "NAME = '北京市'"
            }]
        });

        L.supermap
                .queryService(url2)
                .queryBySQL(param, function (serviceResult) {
                    serviceResult.result.recordsets.map(function (record) {
                        resultLayer = L.geoJSON(record.features, {
                            coordsToLatLng: function (coords) {
                                var latlng = L.CRS.EPSG3857.unproject(L.point(coords[0], coords[1]));
                                latlng.alt = coords[2];
                                return latlng;
                            }
                        }).on('click', function (e) {
                            var city = e.layer.feature.properties.NAME;
                            chart.setOption({
                                title: {
                                    text: city + '商品销量'
                                },
                                series: [
                                    {
                                        name: '商品类目',
                                        data: categoryDataMap[city]
                                    },
                                    {
                                        name: '商品',
                                        data: dataMap[city]
                                    }
                                ]
                            });
                            map.panTo(e.latlng);
                        }, {maxWidth: 700}).addTo(map);
                    })
                });
    }
    function clearLayer() {
        if (resultLayer) {
            resultLayer.removeFrom(map);
        }
    }
    L.DomEvent
            .on(div, 'mouseover', function () {
                map.dragging.disable();
                map.scrollWheelZoom.disable();
                map.doubleClickZoom.disable();
            })
            .on(div, 'mouseout', function () {
                map.dragging.enable();
                map.scrollWheelZoom.enable();
                map.doubleClickZoom.enable();
            })
</script>
</body>
</html>